<?php
include_once $_SERVER["DOCUMENT_ROOT"].'/service/entite/Entite.php';
include_once $_SERVER["DOCUMENT_ROOT"].'/persistence/autregroupe/AutreGroupeDAO.php';
include_once $_SERVER["DOCUMENT_ROOT"].'/outils/database/Database.php';

PropertiesLoader::loadProperties($_SERVER["DOCUMENT_ROOT"].'/properties.ini');
 
class ConcertDAO {
	
	public static function save(ConcertEntite $concertToSave) {
		if($concertToSave->id == Entite::getUnpersistedId()) {
			return ConcertDAO::insert($concertToSave);
		} else {
			return ConcertDAO::update($concertToSave);
		}
	}
	
	public static function delete($id) {
		
		$result =  Database::getInstance()->executeUpdateQueryAndClose("DELETE FROM concert WHERE id = $id") ;
		Database::getInstance()->executeUpdateQueryAndClose("DELETE FROM concert_autregroupe WHERE id_concert = $id") ;
		return $result;
	}
	
	public static function getProchainConcert() {
		$concerts = ConcertDAO::getConcerts(">=", "LIMIT 1", "ASC");
		if (count($concerts) == 0) {
			return null;
		} else {
			return $concerts[0];
		}
	}
	
	public static function getListeConcertAVenir() {
		return ConcertDAO::getConcerts(">=",'', "ASC");
	}
	
	public static function getListeConcertPasse() {
		return ConcertDAO::getConcerts("<",'', 'DESC');
	}
	
	public static function getConcertByID($id) {
		
		$result = Database::getInstance()->executeQueryAndClose("SELECT id, DATE_FORMAT(date, '%d/%m/%Y') as dateEuro,prix,lieu,description,heure, id_albumphoto FROM concert WHERE id =$id");
		foreach ($result as $row) {
			$concert = ConcertDAO::getConcertFromDatabaseRow($row);
			$concert = ConcertDAO::fillAutresGroupes($concert);
			return $concert;
		}
		return null;
	}
	
	private static function fillAutresGroupes($concert) {
		
		$autresGroupesIDResult = Database::getInstance()->executeQueryAndClose("SELECT groupe.id, groupe.nom, groupe.site, groupe.disponible FROM concert_autregroupe lien, autregroupe groupe WHERE lien.id_concert=$concert->id AND lien.id_autregroupe=groupe.id ORDER BY groupe.nom");
		foreach ($autresGroupesIDResult as $autreGroupeID) {
			$concert->autresGroupes[] = AutreGroupeDAO::getAutreGroupeFromDatabaseRow($autreGroupeID);
		}
		return $concert;
	}
	
	private static function getConcertFromDatabaseRow($row) {
		$concert = new ConcertEntite;
		$concert->id = $row['id'];
		$concert->date = $row['dateEuro'];
		$concert->lieu = $row['lieu'];
		$concert->heure = $row['heure'];
		$concert->prix = $row['prix'];
		$concert->description = $row['description'];
		$concert->albumPhoto = AlbumPhotoDAO::getAlbumPhotoByID($row['id_albumphoto']);
		return $concert;
	}
	
	private static function insert(ConcertEntite $concertToSave) {
		
		$albumPhotoEntite = $concertToSave->albumPhoto;
		$date = $concertToSave->date;
		$prix = mysql_escape_string ($concertToSave->prix);
		$lieu = mysql_escape_string ($concertToSave->lieu);
		$description = mysql_escape_string ($concertToSave->description);
		$heure = mysql_escape_string ($concertToSave->heure);
		$concertToSave->id = Database::getInstance()->executeInsertQuery("INSERT INTO concert (date,prix,lieu,description,heure, id_albumphoto) values (STR_TO_DATE('$date','%d/%m/%Y'),'$prix','$lieu','$description','$heure',$albumPhotoEntite->id)") ;
		foreach ($concertToSave->autresGroupes as $i => $groupe) {
			$groupeID = $groupe->id;
			Database::getInstance()->executeInsertQuery("INSERT INTO concert_autregroupe (id_concert,id_autregroupe) values ($concertToSave->id,$groupeID)") ;
		}		
		$cheminAlbumPhoto = PropertiesLoader::getProperty("albumPhoto.chemin", true);
		mkdir($_SERVER["DOCUMENT_ROOT"]."$cheminAlbumPhoto/$concertToSave->id");
		return $concertToSave;
	}
	
	private static function update(ConcertEntite $concertToSave) {
		
		$albumPhotoEntite = $concertToSave->albumPhoto;
		$date = $concertToSave->date;
		$prix = mysql_escape_string ($concertToSave->prix);
		$lieu = mysql_escape_string ($concertToSave->lieu);
		$description = mysql_escape_string ($concertToSave->description);
		$heure = mysql_escape_string ($concertToSave->heure);
		$result = Database::getInstance()->executeUpdateQuery("UPDATE concert SET date = STR_TO_DATE('$date','%d/%m/%Y'),prix = '$prix',lieu = '$lieu',description = '$description',heure = '$heure', id_albumphoto = $albumPhotoEntite->id WHERE id = $concertToSave->id");
		$result = Database::getInstance()->executeUpdateQuery("DELETE FROM concert_autregroupe WHERE id_concert = $concertToSave->id") ;
		foreach ($concertToSave->autresGroupes as $i => $groupe) {
			$groupeID = $groupe->id;
			$result = Database::getInstance()->executeUpdateQuery("INSERT INTO concert_autregroupe (id_concert,id_autregroupe) values ($concertToSave->id,$groupeID)") ;
		}
		mysql_close();
		return $result;
	}
	
	private static function getConcerts($timeComparator, $limit, $order) {
		
		$result = Database::getInstance()->executeQueryAndClose("SELECT id, DATE_FORMAT(date, '%d/%m/%Y') as dateEuro,prix,lieu,description,heure, id_albumphoto FROM concert WHERE date $timeComparator CURRENT_DATE ORDER BY date  $order $limit");
		$concertArray = Array();
		foreach ($result as $row) {
			$concert = ConcertDAO::getConcertFromDatabaseRow($row);
			$concert = ConcertDAO::fillAutresGroupes($concert);
			
			$concertArray[] = $concert;
		}
		return $concertArray;
	}
	
	public static function getListeConcert() {
		
		$result = Database::getInstance()->executeQueryAndClose("SELECT id, DATE_FORMAT(date, '%d/%m/%Y') as dateEuro,prix,lieu,description,heure, id_albumphoto FROM concert ORDER BY date DESC");
		//while (foreach ($result as $row) {){
		foreach ($result as $row) {
			$concert = ConcertDAO::getConcertFromDatabaseRow($row);
			$concert = ConcertDAO::fillAutresGroupes($concert);
			$listeConcert[] = $concert;
		}
		return $listeConcert;
	}
	
	public static function getConcertLieAAlbum($idAlbumPhoto) {
		
		$result = Database::getInstance()->executeQueryAndClose("SELECT id, DATE_FORMAT(date, '%d/%m/%Y') as dateEuro,prix,lieu,description,heure, id_albumphoto FROM concert WHERE id_albumphoto =$idAlbumPhoto");
		//foreach ($result as $row) {
		foreach ($result as $row) {
			$concert = ConcertDAO::getConcertFromDatabaseRow($row);
			$concert = ConcertDAO::fillAutresGroupes($concert);
			return $concert;
		}
		return null;
	}
}
?>